// Google Chrome Game Tools
// Copyright (c) 2010 Joseph Huckaby

var Port = {
	
	planes: [],
	scaleUp: false,
	scaleDown: true,
	
	init: function() {
		this.div = $('game_port');
		assert( !!this.div, "Could not locate game port" );
		
		this.div.style.width = '' + Game.width + 'px';
		this.div.style.height = '' + Game.height + 'px';
	},
	
	resize: function(e) {
		// zoom port to fit window
		if (this.div) {
			var size = getInnerWindowSize();
			var horizScaleFactor = size.width / Game.width;
			var vertScaleFactor = size.height / Game.height;
			var scaleFactor = Math.min( horizScaleFactor, vertScaleFactor );
			
			if ((scaleFactor > 1.0) && !this.scaleUp) scaleFactor = 1.0;
			else if ((scaleFactor < 1.0) && !this.scaleDown) scaleFactor = 1.0;
			
			this.div.style.webkitTransform = 'scale(' + scaleFactor + ')';
			
			this.div.style.left = '' + Math.floor( (size.width / 2) - (Game.width / 2) ) + 'px';
			this.div.style.top = '' + Math.floor( (size.height / 2) - (Game.height / 2) ) + 'px';
			
			// orientation
			this.orientation = (size.width > size.height) ? 'landscape' : 'portrait';
			
			// simulate landscape / portrait CSS links for browsers that don't support it
			var links = document.getElementsByTagName("link");
			var oris = ['landscape', 'portrait'];
			
			for (var idx = 0, len = links.length; idx < len; idx++) {
				var link = links[idx];
				if (link.getAttribute('rel') == 'stylesheet') {
					var media = '' + (link.getAttribute('media') || '');
					for (var idy = 0, ley = oris.length; idy < ley; idy++) {
						var ori = oris[idy];
						if ((media.indexOf(ori) > -1) && (ori != this.orientation)) link.disabled = true;
						else link.disabled = false;
					} // foreach orientation
				} // is stylesheet
			} // foreach link
		}
	},
	
	addDiv: function(args) {
		// quick-create DIV in port
		var div = $(document.createElement('div'));
		div.id = args.id;
		div.className = args.className || '';
		var sty = div.style;
		sty.position = 'absolute';
		
		if (args.x == 'center') args.x = (Game.width / 2) - (args.width / 2);
		if (args.y == 'center') args.y = (Game.height / 2) - (args.height / 2);
		
		if (args.x) sty.left = '' + args.x + 'px';
		if (args.y) sty.top = '' + args.y + 'px';
		if (args.width) sty.width = '' + args.width + 'px';
		if (args.height) sty.height = '' + args.height + 'px';
		
		sty.opacity = args.opacity || '';
		div.innerHTML = args.html || '';
		this.div.appendChild( div );
		return div;
	},
	
	removeDiv: function(thingy) {
		// remove div from port (pass id or div itself)
		var div = $(thingy);
		if (div) {
			this.div.removeChild( div );
		}
	},
	
	addPlane: function(plane) {
		// add plane to list
		this.planes.push( plane );
		plane.init();
	},
	
	removeAll: function() {
		// remove all planes
		for (var idx = 0, len = this.planes.length; idx < len; idx++) {
			this.planes[idx].destroy();
		}
		this.planes = [];
	},
	
	logic: function() {
		// logic loop
		for (var idx = 0, len = this.planes.length; idx < len; idx++) {
			this.planes[idx].logic();
		}
	},
	
	draw: function() {
		// draw loop
		for (var idx = 0, len = this.planes.length; idx < len; idx++) {
			this.planes[idx].draw();
		}
	}
	
};
